package com.sports.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sports.entity.Item;
import com.sports.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @GetMapping("byName")
    //根据名字查找比赛项目
    public List<Item> getItemByName(String name, Integer current){
        return  itemService.getItemListByName(name,current);
    }
    //根据id查找比赛项目
    @GetMapping("{id}")
    public Item getItemById(@PathVariable(value = "id") String id){
        Item item = itemService.getOne(Wrappers.<Item>lambdaQuery().eq(Item::getItemId,id));
        return item;
    }
    //分页显示所有项目
    @GetMapping("list/{current}/{size}")
    public Page<Item> getItemList(String name, @PathVariable(value = "current") Integer current, @PathVariable(value = "size") Integer size){

        return   itemService.getItemPageList(name, current,size);
    }

    //添加比赛项目
    @PostMapping("add")
    public Map addItem(@RequestBody Item item){
        Map map = new HashMap<>();
        if(itemService.getOne(Wrappers.<Item>lambdaQuery().eq(Item::getItemId,item.getItemId()))!=null){
            map.put("message","该项目已存在");
            return  map;
        }
        itemService.save(item);
        map.put("message","success");
        return map;
    }
    //修改比赛项目信息
    @PutMapping("update")
    public Map updateItem(@RequestBody Item item){
        Map map = new HashMap<>();
        itemService.updateById(item);
        map.put("message","success");
        return map;
    }
    //删除比赛项目
    @DeleteMapping("{id}")
    public Map deleteItemById(@PathVariable(value = "id") String id){
        Map map = new HashMap<>();
        itemService.removeById(id);
        map.put("message","success");
        return map;
    }

//    //报名比赛项目
//    @GetMapping("signup")
//    public Map signup(Integer athletesId,Integer itemId){
//        Map map = new HashMap<>();
//        if (!itemService.signUp(athletesId,itemId)){
//            map.put("message","报名失败");
//            return map;
//        }
//        map.put("message","报名成功");
//        return map;
//    }

}